<?php

App::uses('AppController', 'Controller');

class NewsController extends AppController {

	var $name = 'CompanyNew';
	var $uses = array('CompanyNew');


	function detail($id = null) {
		$this->set('news', $this->CompanyNew->read(array(), (int)$id));

		$view = new View( $this );
        $Text = $view->loadHelper( 'Text' );

		$this->paginate = array( 'order'=> array('sort' => 'asc'), 'limit' => 5);
        $items = $this->paginate(array('status' => 4, 'id <>' => (int)$id ));
        if(!empty($items))
        foreach($items as $k => $v){
        	$v['CompanyNew']['desc'] =  strip_tags($v['CompanyNew']['desc']);
        	$v['CompanyNew']['desc'] = $Text->truncate($v['CompanyNew']['desc'], 200, array('ellipsis' => '...', 'exact' => false, 'html' => true));
			$items[$k] = $v;
        }
		$this->set('items', $items);
	}

	function index() {
		$view = new View( $this );
        $Text = $view->loadHelper( 'Text' );

		$this->paginate = array( 'order'=> array('sort' => 'asc'), 'limit' => 10);
        $items = $this->paginate(array('status' => 4));
        if(!empty($items))
        foreach($items as $k => $v){
        	$v['CompanyNew']['desc'] =  strip_tags($v['CompanyNew']['desc']);
        	$v['CompanyNew']['desc'] = $Text->truncate($v['CompanyNew']['desc'], 200, array('ellipsis' => '...', 'exact' => false, 'html' => true));
			$items[$k] = $v;
        }
		$this->set('items', $items);
	}

	/**
	 * Danh sach san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_index() {
		$view = new View( $this );
        $Text = $view->loadHelper( 'Text' );

		$this->paginate = array( 'order'=> array('sort' => 'asc'));
        $items = $this->paginate(array('status' => 4));
        if(!empty($items))
        foreach($items as $k => $v){
        	$v['CompanyNew']['desc'] =  strip_tags($v['CompanyNew']['desc']);
        	$v['CompanyNew']['desc'] = $Text->truncate($v['CompanyNew']['desc'], 150, array('ellipsis' => '...', 'exact' => false, 'html' => true));
			$items[$k] = $v;
        }
		$this->set('items', $items);
	}

	/**
	 * Add
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_add($id = null)
	{
		if(!empty($this->request->data))
		{
			$data = &$this->request->data[$this->als];

			$this->CompanyNew->set($data);
			$errors = $this->CompanyNew->invalidFields();

			//Save
			if(empty($errors)) {
				if(!empty($data['image']['name'])) {
					$data['image'] = $this->_upload($data['image']);
					if( isset($data['image_old'] )) {
						$path = PUBLIC_IMAGE_DETAIL.$data['image_old'] ;
						@unlink($path);
						$path = PUBLIC_IMAGE_THUMB.$data['image_old'] ;
						@unlink($path);
					}
				} else {
					if( isset($data['image_old'] )) {
						$data['image'] = $data['image_old'];
						unset($data['image_old']);
					} else {
						$data['image'] = '';
					}
				}


				// kiem tra edit
				if( empty($data['id']) ) {
					unset($data['id']);
				} else {
					$data['id'] = (int)$data['id'];
				}

				$data['status'] = 4;

				if($this->CompanyNew->save($data, false)) {

					$this->Session->setFlash('Lưu tin tức thành công.', 'success');
					$this->redirect('/admin/news' );
				} else {
					$this->Session->setFlash('Lưu tin tức thất bại. Vui lòng xem lại thông tin bên dưới', 'error');
				}
			} else {
				$this->Session->setFlash('Lưu tin tức thất bại.', 'error');
			}
		}

		// Truong hop show edit
		if( !empty( $id )) {
			$info = $this->CompanyNew->read(array(),(int)$id);
			if(!empty($info)) {
				$this->request->data = $info;
			}
		}
	}

	/**
	 * delete san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_delete($id = null){
		if(!empty($id)) {
			$id = (int)$id;
			$info = $this->CompanyNew->read(array(),$id);
			if( $this->CompanyNew->delete($id)) {

				$path = PUBLIC_IMAGE_DETAIL.$info['CompanyNew']['image'] ;
				@unlink($path);
				$path = PUBLIC_IMAGE_THUMB.$info['CompanyNew']['image'] ;
				@unlink($path);

				$this->Session->setFlash('Xóa tin tức thành công.', 'success');
			} else {
				$this->Session->setFlash('Xóa tin tức thất bại. Vui lòng thử lại sao', 'error');
			}
		} else {
			$this->Session->setFlash('Không tìm thấy tin tức. Vui lòng thử lại sao', 'error');
		}
		$this->redirect('/admin/news');
	}

		/**
	 * Change Sort
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_changeSort() {
		if(!empty($this->request->data['id']) && !empty($this->request->data['sort'])) {
			$this->Category->id = (int)$this->request->data['id'];

			if($this->Category->saveField('sort', (int)$this->request->data['sort']))
				$this->_responseJson(array('code' => 'success'));
			else
				$this->_responseJson(array('code' => 'danger'));
		}
		$this->_responseJson(array('code' => 'danger'));
	}


}
